package com.lintesis.lintesismodel.users;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.HashSet;
import java.util.List;
import java.util.Vector;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.ManyToMany;

import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import org.hibernate.annotations.Parameter;
import org.hibernate.annotations.Type;
import org.hibernate.annotations.TypeDef;
import org.jasypt.hibernate.type.EncryptedStringType;

import com.lintesis.lintesismodel.common.hibernate.Report;



@TypeDef(name = "encryptedString", typeClass = EncryptedStringType.class, parameters = { @Parameter(name = "encryptorRegisteredName", value = "hibernateStringEncryptor") })
@Entity
public class User extends Person
{

	@Column(unique = true)
	private String login;
	@Type(type = "encryptedString")
	private String password;
	private Calendar entryDate;
	private Calendar endDate;
	private Boolean userState;
	//private String workerFile;
	@Type(type = "encryptedString")
	private String accessPass;

//	@ManyToOne
//	private Company company;

	@ManyToMany
	private List<Role> rols=new ArrayList<Role>();

	@Column(unique = true)
	private Integer terminalLogin;
	
	public Integer getTerminalLogin()
	{
		return terminalLogin;
	}

	public void setTerminalLogin(Integer terminalLogin)
	{
		this.terminalLogin = terminalLogin;
	}

	public User()
	{
		// rols = new ArrayList<Role>();
	}
//	public ArrayList<TerminalRight> getTerminalRights()
//	{
//		ArrayList<TerminalRight> rights = new ArrayList<TerminalRight>();
//		for (Role role : this.getRols())
//		{
//			for (TerminalRight term : role.getTerminalRights())
//			{
//				if (!rights.contains(term))
//					rights.add(term);
//			}
//		}
//		return rights;
//	}
//	public TerminalRight[] getTerminalRightsNativeArray()
//	{
//		TerminalRight[] p = new TerminalRight[0];
//		if (p != null)
//			p = this.getTerminalRights().toArray(p);
//		return p;   
//	}
	public Vector<String> getUsrRights()
	{
		Vector<String> grantedAuthoritySet = new Vector<String>();
		for (Role role : rols)
		{
			grantedAuthoritySet.add(role.getRoleName());
			grantedAuthoritySet.addAll(role.getStringUsrRight());
		}
		return grantedAuthoritySet;
	}

	public boolean hasRole(Role role)
	{
		for (Role aux : this.getRols())
			if (aux.getIdRol().equals(role.getIdRol()))
				return true;
		return false;
	}

//	public ArrayList<Station> getAllowedStations()
//	{
//		ArrayList<Station> stations = new ArrayList<Station>();
//		for (Role role : this.getRols())
//		{
//			for (Station sta : role.getStations())
//			{
//				if (!stations.contains(sta))
//					stations.add(sta);
//			}
//		}
//		return stations;
//	}
//
//	public ArrayList<Company> getAllowedCompanies()
//	{
//		ArrayList<Company> companies = new ArrayList<Company>();
//		for (Role role : this.getRols())
//		{
//			for (Company cmp : role.getCompanies())
//			{
//				if (!companies.contains(cmp))
//					companies.add(cmp);
//			}
//		}
//		return companies;
//	}

	public ArrayList<Integer> getAllowedReportsIds()
	{

		ArrayList<Integer> reportsIds = new ArrayList<Integer>();
		for (Role role : this.getRols())
		{
			for (Report rep : role.getReports())
			{
				if (!reportsIds.contains(rep.getId()))
					reportsIds.add(rep.getId());
			}
		}

		return reportsIds;
	}

	public Role[] getRolsNativeArray()
	{
		Role[] p = new Role[0];
		if (p != null)
			p = this.rols.toArray(p);
		return p;
	}

	public void setRolsNativeArray(Role[] r)
	{
		this.rols = new ArrayList<Role>(Arrays.asList(r));
	}

	public List<Role> getRols()
	{
		return rols;
	}

	public void setRols(List<Role> rols)
	{
		this.rols = rols;
	}

	public void setRols(HashSet<Role> rols)
	{
		this.rols = new ArrayList<Role>(rols);
	}

	public void setLogin(String login)
	{
		this.login = login;
	}

	public String getPassword()
	{
		return password;
	}

	public void setPassword(String password)
	{
		this.password = password;
	}

	public String getLogin()
	{
		return login;
	}

	public Calendar getEntryDate()
	{
		return entryDate;
	}

	public void setEntryDate(Calendar entryDate)
	{
		this.entryDate = entryDate;
	}

	public Calendar getEndDate()
	{
		return endDate;
	}

	public void setEndDate(Calendar endDate)
	{
		this.endDate = endDate;
	}

	public Boolean getUserState()
	{
		return userState;
	}

	public Boolean getEnabled()
	{
		return userState;
	}

	public Boolean isUserState()
	{
		return userState;
	}
	
	public void setUserState(Boolean userState)
	{
		this.userState = userState;
	}

//	public String getWorkerFile()
//	{
//		return workerFile;
//	}
//
//	public void setWorkerFile(String workerFile)
//	{
//		this.workerFile = workerFile;
//	}

	public String getAccessPass()
	{
		return accessPass;
	}

	public void setAccessPass(String accessPass)
	{
		this.accessPass = accessPass;
	}


//	public Company getCompany()
//	{
//		return company;
//	}
//
//	public void setCompany(Company company)
//	{
//		this.company = company;
//	}

	public String getRolsDescription()
	{
		StringBuffer sb = new StringBuffer();
		int i = 0;
		if(rols==null)
			return sb.toString();
		for (Role r : rols)
		{
			i++;
			if (i < rols.size())
			{
				sb.append(r.getRoleName() + ",");
			} else
			{
				sb.append(r.getRoleName());
			}

		}
		return sb.toString();
	}

	@Override
	public void doAfterDelete()
	{
		// TODO Auto-generated method stub

	}

	@Override
	public void doAfterSave()
	{
//		try
//		{
//			WorkerSpreadImporter wsi = new WorkerSpreadImporter();
//			wsi.setSession(this.obtainHibernateSession());
//			wsi.executeImport();
//		} catch (Exception e)
//		{
//			new RuntimeException("Imposible exportar workers", e);
//		}
	}

	@Override
	public void doBeforeDelete()
	{
		// TODO Auto-generated method stub

	}

	@Override
	public void doBeforeSave()
	{
		// TODO Auto-generated method stub

	}
	//
	// @Override
	// public boolean equals(Object object) {
	// boolean retorno = false;
	// try {
	// retorno = ReflectionUtils.compareObjects(this, object, 2);
	// } catch (Exception e) {
	// e.printStackTrace();
	// }
	// return retorno;
	// }
	
//	public static void exportWorkers()
//	{
//		try
//		{
//			WorkerSpreadImporter wsi = new WorkerSpreadImporter();
//			wsi.setSession(ConnectionManager.getSession());
//			wsi.executeImport();
//		} catch (Exception e)
//		{
//			new RuntimeException("Imposible exportar workers", e);
//		}
//	}

}
